#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")


#'#
#'# Chargement des dependances
#'# 

source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))

DATA_PATH <- file.path(R_RESULTS_PATH, "HRCompare/v5")
DEST_PATH <- file.path(R_RESULTS_PATH, "HRCompare/final")

statsCols <- c("sizeRatio", "overlapIndex", "propInRef", "comparedArea", "refArea")

#'#
#'# Chargement des donnees
#'# 

hrStatsRaw <- fread(file.path(DATA_PATH, "HR_compare_raw.csv"))[, type := "Raw"]
hrStatsLC32 <- fread(file.path(DATA_PATH, "HR_compare_LC32.csv"))[, type := "LC32"]
hrStatsLC321 <- fread(file.path(DATA_PATH, "HR_compare_LC321.csv"))[, type := "LC321"]
hrStatsMb <- fread(file.path(DATA_PATH, "HR_compare_mb.csv"))[, type := "DAF"]
hrStatsFilter <- fread(file.path(DATA_PATH, "HR_compare_filter.csv"))[, type := "SF"]


hrTracksFilter <- rbindlist(list(hrStatsRaw, hrStatsLC32, hrStatsLC321,
        hrStatsMb, hrStatsFilter))
hrTracksFilter[, type := factor(type, levels=c("Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]
hrTracksFilter[, deployment := as.factor(deployment)]
hrTracksFilter[, method := as.factor(method)]
setkey(hrTracksFilter, method, percent, type)

save(hrTracksFilter, file=file.path(DATA_PATH, "hrTracksFilter.RData"))


#'#
#'# Donnees pour graphiques (moyennes globales sur toutes les donnees)
#'# Filtres : Utilisation de moyenne du second ordre.
#'# 


res <- NULL
for (i in statsCols) {
  res <- rbind(res, cbind(hrTracksFilter[, meanICl2(.SD, i, byG2="deployment"), by=list(type, method, percent)], stat=i))
}

#' Ajout de la taille de reference
ref <- hrTracksFilter[, meanICl2(.SD, "refArea", byG2="deployment"), by=list(method, percent)]
ref[, type := "Ref"]
ref[, stat := "comparedArea"]
setcolorder(ref, colnames(res))

hrGraphsTracksFilter <- rbindlist(list(res, ref))
hrGraphsTracksFilter[, type := factor(type, levels=c("Ref", "Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]
hrGraphsTracksFilter[, stat := as.factor(stat)]

save(hrGraphsTracksFilter, file=file.path(DEST_PATH, "hrGraphsTracksFilter.RData"))
#write.csv(res, file.path(DEST_PATH, "hrGraphsTracksFilter.csv"), row.names=FALSE)

#'#
#'# Donnees pour statistiques (moyennes par deploiement)
#'# 

#hrStatsTracks <- hrStatsTracks[sizeRatio < 200]
hrStatsTracksFilter <- compileHRStats(hrTracksFilter, statsCols, statBy=quote(list(type, method, percent, deployment)))
setkey(hrStatsTracksFilter, method, percent)
save(hrStatsTracksFilter, file=file.path(DEST_PATH, "hrStatsTracksFilter.RData"))
#write.csv(statsTracksFilter, file.path(R_RESULTS_PATH, "HRCompare/HRstatsTracksFilter.csv"), row.names=FALSE)






